package com.jueban.serviceImpl;

import com.jueban.Dto.*;
import com.jueban.Vo.ApplyOfIPVo;
import com.jueban.Vo.ApplyOfIPsVo;
import com.jueban.Vo.ResultVo;
import com.jueban.model.ApplyOfIP;
import com.jueban.model.State;
import com.jueban.model.Suggest;
import com.jueban.model.user.User;
import com.jueban.repository.ApplyOfIPRepository;
import com.jueban.service.ApplyOfIPService;
import com.jueban.service.FileService;
import com.jueban.service.UserService;
import com.jueban.util.Codes;
import com.jueban.util.Enum.JobType;
import com.jueban.util.Enum.UserType;
import com.jueban.util.Messages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@Service
public class ApplyOfIPServiceImpl implements ApplyOfIPService {

    private final ApplyOfIPRepository applyOfIPRepository;

    private final UserService userService;

    private final FileService fileService;


    @Autowired
    public ApplyOfIPServiceImpl
            (ApplyOfIPRepository applyOfIPRepository, UserService userService, FileService fileService) {
        this.applyOfIPRepository = applyOfIPRepository;
        this.userService = userService;
        this.fileService = fileService;
    }


    /*-----------------------------------------READ------------------------------------------------*/

    /**
     * 根据id获取申请,若有权限返回(该函数返回的是申请的详细内容,
     * 获取批量的申请只有id,标题等内容可以根据id获取详细内容)
     *
     * @param id id
     * @return 申请信息
     */
    @Override
    @PreAuthorize("isAuthenticated()")
    public ApplyOfIPVo getApplyOfIP(int id) {
        //根据id查询申请
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(id);
        ApplyOfIPVo resultVo = new ApplyOfIPVo();
        //诺申请存在则根据进行查询的用户的权限返回结果
        if (applyOfIP != null) {
            //诺该用户不能获取该(根据id查询的申请)申请返回查询失败信息
            if (!hasAuth(applyOfIP)) {
                resultVo.setMessages(Messages.CANNOT_GET_APPLY_OF_IP);
                resultVo.setFailed();
                return resultVo;
            }
            //返回申请详细信息
            resultVo.setProperties(applyOfIP);
            resultVo.setStatus_codes(Codes.SUCCEEDED);
            resultVo.setMessages(Messages.GET_SUCCEED);
        } else {
            //该用户没有权限返回失败结果
            resultVo.setStatus_codes(Codes.FAIL);
            resultVo.setMessages(Messages.GET_FAIL);
        }
        return resultVo;
    }

    @Override
    public File getApplyOfIPApplyFile(int id) {
        //根据id查询申请
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(id);

        //若查询到的申请不为空和操作人有权限获取申请资料则返回文件
        if (applyOfIP != null) {
            if (hasAuth(applyOfIP)) {
                return fileService.getApplyFile(applyOfIP);
            }
        }
        return null;
    }


    /**
     * 判断用户是否拥有获取该申请的权限
     *
     * @param applyOfIP 申请
     * @return 是否能返回该申请给这个用户
     */
    private boolean hasAuth(ApplyOfIP applyOfIP) {
        //获取进行该操作的用户
        User user = userService.getUser();

        //诺没有登陆返回false
        if (user == null) return false;
        //诺操作的用户为申请的申请人返回true
        if (applyOfIP.getApplicant().getId().equals(user.getId())) return true;

        //若操作人为老师根据职位返回结果
        if (user.getUserType().equals(UserType.teacher)) {
            switch (user.getJobType()) {
                case manager:
                case departmentHead:
                case leader:
                case chargeOfIP:
                    return true;
                default:
                    return false;
            }
        }
        //若操作人为管理员返回true否则返回false
        return user.getUserType().equals(UserType.admin);
    }

    /*
    @Override
    @Secured({"ROLE_admin","ROLE_departmentHead"})
    public ApplyOfIPsVo findApplyOfIP(ApplyOfIPQueryDto applyOfIPQueryDto) {
        //设置分页
        Pageable pageable = new PageRequest(
                applyOfIPQueryDto.getOffset(), applyOfIPQueryDto.getPageSize());
        String queryType = applyOfIPQueryDto.getQueryType();

        //根据查询类型查询
        if (queryType.equals("teacher")) {
            return findTeacherApplyOfIP(pageable, applyOfIPQueryDto);
        } else if (queryType.equals("admin")) {
            return getAdminApplyOfIP(pageable, applyOfIPQueryDto);
        } else if (queryType.equals("self")) {
            return getSelfApplyOfIP(pageable, applyOfIPQueryDto);
        } else if (queryType.equals("manager")) {
            return getAdminApplyOfIP(pageable, applyOfIPQueryDto);
        }

        ApplyOfIPsVo applyOfIPsVo = new ApplyOfIPsVo();
        applyOfIPsVo.setFailed();
        applyOfIPsVo.setMessages(Messages.GET_FAIL);
        return applyOfIPsVo;
    }*/


    @Override
    @Secured({"ROLE_admin","ROLE_manager","ROLE_departmentHead","ROLE_chargeOfIP","ROLE_leader","ROLE_personnel"})
    public ApplyOfIPsVo findTeacherApplyOfIP(Pageable pageable, ApplyOfIPQueryDto applyOfIPQueryDto) {
        User user = userService.getUser();
        String department = user.getDepartment();

        if (applyOfIPQueryDto.getIsFindAll() == null) {
            ApplyOfIPsVo resultVo = new ApplyOfIPsVo();
            resultVo.setMessages(Messages.QUERY_TYPE_ERROR);
            resultVo.setFailed();
            return resultVo;
        }

        if (applyOfIPQueryDto.getIsFindAll()) {
            return getTeacherApplyOfIPByDepartment(pageable, department);
        } else {
            return getTeacherAuditApplyOfIPByDepartment(pageable, applyOfIPQueryDto.getIsAudit());
        }
    }


    //TODO 管理员搜索知识产权申请类型,排序


    @Override
    @Secured({})
    public ApplyOfIPsVo getSelfApplyOfIP(Pageable pageable, ApplyOfIPQueryDto applyOfIPQueryDto) {
        Page<ApplyOfIP> applyOfIPs;
        ApplyOfIPsVo result;
        User user = userService.getUser();
        //根据类型查询
        if (transferBoolean(applyOfIPQueryDto.getIsFullAudited())) {
            applyOfIPs = applyOfIPRepository
                    .findApplyOfIPSByApplicantWithState(pageable, user, State.FULL_AUDITED_HASHCODE);
            result = new ApplyOfIPsVo(applyOfIPs);

        } else {
            applyOfIPs = applyOfIPRepository
                    .findApplyOfIPSByApplicantQuery(pageable,user,applyOfIPQueryDto.getApplyName());
            result = new ApplyOfIPsVo(applyOfIPs);
        }
        result.setSucceeded();
        result.setMessages(Messages.GET_SUCCEED);
        return result;
    }


    private ApplyOfIPsVo getTeacherApplyOfIPByDepartment(Pageable pageable, String department) {
        Page<ApplyOfIP> applyOfIPs = applyOfIPRepository
                .findApplyOfIPSByApplicant_DepartmentEquals(pageable, department);
        ApplyOfIPsVo result = new ApplyOfIPsVo(applyOfIPs);
        result.setSucceeded();
        result.setMessages(Messages.GET_SUCCEED);
        return result;
    }

    private ApplyOfIPsVo getTeacherAuditApplyOfIPByDepartment(Pageable pageable, Boolean isAudited) {
        User user = userService.getUser();
        State state = getState(user, isAudited);
        Page<ApplyOfIP> applyOfIPs = applyOfIPRepository
                .findApplyOfIPSWithState(
                        pageable, state.hashCode()
                        , user.getDepartment(), "", "");
        ApplyOfIPsVo result = new ApplyOfIPsVo(applyOfIPs);
        result.setSucceeded();
        result.setMessages(Messages.GET_SUCCEED);
        return result;
    }

    /**
     * @param pageable 分页
     * @return 结果
     */
    @Override
    @Secured({ "ROLE_admin", "ROLE_manager" })
    public ApplyOfIPsVo getAdminApplyOfIP(
            Pageable pageable, ApplyOfIPQueryDto applyOfIPQueryDto) {
        Page<ApplyOfIP> applyOfIPs;
        State state = applyOfIPQueryDto.getState();
        String department = applyOfIPQueryDto.getDepartment();
        String applyName = applyOfIPQueryDto.getApplyName();
        String applicantName = applyOfIPQueryDto.getApplicantName();
        if (state == null) {
            applyOfIPs = applyOfIPRepository
                    .findApplyOfIPS(pageable, department, applyName, applicantName);
        } else {
            applyOfIPs = applyOfIPRepository.findApplyOfIPSWithState(
                    pageable, state.hashCode(), department, applyName, applicantName
            );
        }
        ApplyOfIPsVo result = new ApplyOfIPsVo(applyOfIPs);
        result.setSucceeded();
        result.setMessages(Messages.GET_SUCCEED);
        return result;
    }

    @Override
    @PreAuthorize(value = "hasRole('ROLE_admin') or hasRole('ROLE_manager')")
    public File getProxyFile(int id) {
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(id);
        return fileService.getProxyFile(applyOfIP);
    }

    @Override
    @PreAuthorize(value = "hasRole('ROLE_admin') or hasRole('ROLE_manager')")
    public File getAuthorizedFile(int id) {
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(id);
        return fileService.getAuthorizedFile(applyOfIP);
    }
    /*--------------------------------------CREATE-----------------------------------------------*/

    /**
     * 申请知识产权
     *
     * @param applyOfIPDto 申请资料
     * @return 操作结果
     */
    @Override
    @PreAuthorize("isAuthenticated()")
    public ResultVo addApplyOfIP(ApplyOfIPDto applyOfIPDto, MultipartFile file) {
        ResultVo resultVo = new ResultVo();
        //获取操作用户
        User user = userService.getUser();
        //创建申请并保存
        ApplyOfIP applyOfIP = new ApplyOfIP();
        applyOfIP.setApplicant(user);
        applyOfIPDto.transferData(applyOfIP);
        //设置最终专利名(可修改)
        applyOfIP.setFinallyIPName(applyOfIP.getApplyInfo().getApplyIPName());
        applyOfIP.getApplyInfo().setInventorType(user.getUserType().toString());
        applyOfIP.getApplyInfo().setDepartment(user.getDepartment());
        //保存文件
        if (!fileService.saveApplyFile(applyOfIP, file)) {
            resultVo.setFailed();
            resultVo.setMessages(Messages.UPLOAD_FILE_FAIL);
            return resultVo;
        }
        applyOfIPRepository.save(applyOfIP);

        //诺操作成功返回操作结果
        if (applyOfIP.getId() != null) {
            resultVo.setSucceeded();
            resultVo.setMessages(Messages.ADD_SUCCEED);
        } else {
            resultVo.setFailed();
            resultVo.setMessages(Messages.ADD_FAIL);
        }
        return resultVo;
    }

    /*---------------------------------------DELETE------------------------------------------*/

    /**
     * 根据id删除申请
     *
     * @param id id
     * @return 操作结果
     */
    @Override
    @PreAuthorize("isAuthenticated()")
    public ResultVo deleteApplyOfIP(int id) {
        ResultVo resultVo = new ResultVo();
        User user = userService.getUser();
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(id);
        if (applyOfIP.getApplicant() == user || user.getUserType().equals(UserType.admin)) {
            try {
                applyOfIPRepository.delete(id);
                resultVo.setSucceeded();
                resultVo.setMessages(Messages.DEL_SUCCEED);
            } catch (Exception e) {
                e.printStackTrace();
                resultVo.setMessages(Messages.DEL_FAIL);
                resultVo.setFailed();
            }
            return resultVo;
        } else {
            resultVo.setFailed();
            resultVo.setMessages(Messages.CANNOT_DELETE_APPLY_OF_IP);
        }
        return resultVo;
    }


    /*----------------------------------------UPDATE---------------------------------------*/

    /**
     * 审核申请
     *
     * @param suggestAuditDto 审核结果
     * @return 操作结果
     */
    @Override
    @PreAuthorize("hasRole('ROLE_admin') or hasRole('ROLE_departmentHead') " +
            "or hasRole('ROLE_chargeOfIP') or hasRole('ROLE_manager') ")
    public ResultVo auditApply(SuggestAuditDto suggestAuditDto) {
        //获取当前进行操作的用户
        User user = userService.getUser();

        ResultVo resultVo = new ResultVo();
        //获取要操作的申请的id
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(suggestAuditDto.getApplyId());
        //若找不到申请直接返回操作失败结果
        if (applyOfIP == null) {
            resultVo.setMessages(Messages.FIND_APPLYOFIP_BY_ID_NOT_FOUND);
            resultVo.setFailed();
            return resultVo;
        }

        //判断用户类型
        if (user.getUserType().equals(UserType.teacher)) {
            //判断老师的职位并进行相应的审核
            switch (user.getJobType()) {
                case departmentHead:
                    resultVo = departmentAudit(suggestAuditDto, user, applyOfIP);
                    break;
                case manager:
                    resultVo = managerAudit(suggestAuditDto, user, applyOfIP);
                    break;
                case chargeOfIP:
                    resultVo = academicAudit(suggestAuditDto, applyOfIP);
                    break;
                default:
                    //该老师没有权限审核
                    resultVo.setMessages(Messages.HAS_NO_POWER);
                    resultVo.setFailed();
            }
            //若为管理员执行下面操作
        } else if (user.getUserType().equals(UserType.admin)) {
            resultVo = adminAudit(suggestAuditDto, user, applyOfIP);
        } else {
            //没有权限
            resultVo.setMessages(Messages.HAS_NO_POWER);
            resultVo.setFailed();
        }

        return resultVo;
    }


    /**
     * 部门审核
     *
     * @param suggestAuditDto 审核结果
     * @param user            该用户为部长
     * @param applyOfIP       审核的申请
     * @return 操作结果
     */
    private ResultVo departmentAudit
    (SuggestAuditDto suggestAuditDto, User user, ApplyOfIP applyOfIP) {

        //设置申请的部门申请状态和部门审核意见,审核人
        applyOfIP.getState().setIsDepartmentAuditedPass(suggestAuditDto.getIsPass());
        Suggest suggest = Suggest.makeSuggest(suggestAuditDto.getSuggest(), user);
        applyOfIP.setDepartmentSuggest(suggest);

        //保存操作完的申请 返回ResultVo
        applyOfIPRepository.save(applyOfIP);
        ResultVo resultVo = new ResultVo();
        resultVo.setSucceeded();
        resultVo.setMessages(Messages.DEPARTMENT_AUDIT_SUCCEED);
        return resultVo;
    }

    /**
     * 科研处审核
     *
     * @param suggestAuditDto 审核结果
     * @param applyOfIP       审核的申请
     * @return 操作结果
     */
    private ResultVo academicAudit
    (SuggestAuditDto suggestAuditDto, ApplyOfIP applyOfIP) {
        //获取申请的审核状态
        State state = applyOfIP.getState();

        ResultVo resultVo = new ResultVo();

        //判断前置审核状态是否已经通过
        if (transferBoolean(state.getIsDepartmentAuditedPass())
                && transferBoolean(state.getIsManagerAuditedPass())) {
            //设置科研处审核状态并保存
            state.setIsAcademicAuditedPass(suggestAuditDto.getIsPass());
            applyOfIPRepository.save(applyOfIP);

            //设置操作结果
            resultVo.setSucceeded();
            resultVo.setMessages(Messages.ACADEMIC_AUDIT_SUCCEED);
        } else {
            resultVo.setFailed();
            resultVo.setMessages(Messages.ACADEMIC_AUDIT_FAIL);
        }

        return resultVo;
    }

    /**
     * 知识产权管理员审核
     *
     * @param suggestAuditDto 审核结果
     * @param user            该用户为知识产权管理员
     * @param applyOfIP       审核的申请
     * @return 操作结果
     */
    private ResultVo managerAudit
    (SuggestAuditDto suggestAuditDto, User user, ApplyOfIP applyOfIP) {
        //获取申请的审核状态
        State state = applyOfIP.getState();

        ResultVo resultVo = new ResultVo();
        //判断前置审核状态是否已经通过
        if (transferBoolean(state.getIsDepartmentAuditedPass())) {

            //设置知识产权管理员审核结果并保存
            state.setIsManagerAuditedPass(suggestAuditDto.getIsPass());
            Suggest suggest = Suggest.makeSuggest(suggestAuditDto.getSuggest(), user);
            applyOfIP.setAcademicSuggest(suggest);
            applyOfIPRepository.save(applyOfIP);

            //设置操作结果
            resultVo.setSucceeded();
            resultVo.setMessages(Messages.MANAGER_AUDIT_SUCCEED);
        } else {
            resultVo.setFailed();
            resultVo.setMessages(Messages.MANAGER_CANNOT_AUDIT_BY_DEPARTMENT_NO_AUDIT);
        }
        return resultVo;
    }

    /**
     * 管理员审核
     *
     * @param suggestAuditDto 审核结果
     * @param user            该用户为管理员
     * @param applyOfIP       审核的申请
     * @return 操作结果
     */
    private ResultVo adminAudit
    (SuggestAuditDto suggestAuditDto, User user, ApplyOfIP applyOfIP) {
        //获取管理员要审核的类型
        JobType jobType = JobType.valueOf(suggestAuditDto.getAuditType());

        ResultVo resultVo = new ResultVo();

        //TODO 待改进管理员应能改变任意状态
        //判断要审核的类型并进行审核,设置操作结果
        if (jobType.equals(JobType.departmentHead)) {
            resultVo = departmentAudit(suggestAuditDto, user, applyOfIP);
        } else if (jobType.equals(JobType.manager)) {
            resultVo = managerAudit(suggestAuditDto, user, applyOfIP);
        } else if (jobType.equals(JobType.chargeOfIP)) {
            resultVo = academicAudit(suggestAuditDto, applyOfIP);
        } else {
            resultVo.setFailed();
            resultVo.setMessages(Messages.CANNOT_AUDIT_APPLY_OF_IP);
        }
        return resultVo;
    }


    @Override
    @PreAuthorize("hasRole('ROLE_admin') or hasRole('ROLE_manager')")
    public ResultVo enterProxyAudit(ProxyAuditDto proxyAuditDto, MultipartFile file) {

        ResultVo resultVo = new ResultVo();

        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(proxyAuditDto.getId());
        applyOfIP.setFinallyIPName(proxyAuditDto.getFinalIPName());
        applyOfIP.getState().setIsEnterProxyForm(proxyAuditDto.getIsPass());
        if (proxyAuditDto.getIsPass()) {
            if (fileService.saveProxyFile(applyOfIP, file)) {
                resultVo.setMessages(Messages.ENTER_PROXY_FILE_SUCCESS);
                resultVo.setSucceeded();
                applyOfIPRepository.save(applyOfIP);
            } else {
                resultVo.setFailed();
                resultVo.setMessages(Messages.ENTER_PROXY_FILE_FAIL);
            }
        } else {
            applyOfIPRepository.save(applyOfIP);
            resultVo.setSucceeded();
            resultVo.setMessages(Messages.ENTER_PROXY_FILE_SUCCESS);
        }

        return resultVo;
    }


    @Override
    @PreAuthorize("hasRole('ROLE_admin') or hasRole('ROLE_manager')")
    public ResultVo enterAuthorizedFile(AuthorizedDto authorizedDto, MultipartFile file) {

        ResultVo resultVo = new ResultVo();
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(authorizedDto.getId());

        applyOfIP.getState().setIsAuthorized(authorizedDto.getIsPass());

        if (authorizedDto.getIsPass()) {
            applyOfIP.setIdentifier(authorizedDto.getIdentifier());
            if (fileService.saveAuthorizedFile(applyOfIP, file)) {
                resultVo.setSucceeded();
                resultVo.setMessages(Messages.ENTER_AUTHORIZED_FILE_SUCCESS);
            } else {
                resultVo.setFailed();
                resultVo.setMessages(Messages.ENTER_AUTHORIZED_FILE_FAIL);
            }
        } else {
            resultVo.setSucceeded();
            resultVo.setMessages(Messages.ENTER_AUTHORIZED_FILE_SUCCESS);
        }

        applyOfIPRepository.save(applyOfIP);
        return resultVo;
    }




    /*---------------------------------------OTHER--------------------------------------------*/


    private State getState(User user, Boolean isPass) {
        State state = new State();

        if (user.getUserType().equals(UserType.teacher)) {
            switch (user.getJobType()) {
                case departmentHead:
                    state.setIsDepartmentAuditedPass(isPass);
                    break;
                case manager:
                    state.setIsDepartmentAuditedPass(true);
                    state.setIsManagerAuditedPass(isPass);
                    break;
                case chargeOfIP:
                    state.setIsManagerAuditedPass(true);
                    state.setIsDepartmentAuditedPass(true);
                    state.setIsAcademicAuditedPass(isPass);
                    break;
                default:
                    state = null;
            }
        }
        return state;
    }


    /**
     * 将Boolean类转换为boolean
     *
     * @param b Boolean
     * @return 诺Boolean为null返回false
     */
    private boolean transferBoolean(Boolean b) {
        if (b == null) return false;
        else return b;
    }

}

/*---------------------------------------RECYCLE-BIN--------------------------------------*/

    /*
    private ApplyOfIPsVo getAdminApplyOfIP(Pageable pageable) {
        Page<ApplyOfIP> applyOfIPs = applyOfIPRepository.findAll(pageable);
        ApplyOfIPsVo result = new ApplyOfIPsVo(pageable, applyOfIPs);
        result.setSucceeded();
        result.setMessages(Messages.GET_SUCCEED);
        return result;
    }*/

        /*
    @Override
    @PreAuthorize("hasRole('ROLE_teacher_departmentHead')")
    public ResultVo departmentAudit(DepartmentAuditDto departmentAuditDto) {
        ResultVo resultVo = new ResultVo();
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(departmentAuditDto.getApplyId());

        if (applyOfIP == null) {
            resultVo.setMessages(Messages.FIND_APPLYOFIP_BY_ID_NOT_FOUND);
            resultVo.setFailed();
            return resultVo;
        }
        applyOfIP.getState().setIsDepartmentAuditedPass(departmentAuditDto.getIsPass());
        applyOfIPRepository.save(applyOfIP);
        resultVo.setSucceeded();
        resultVo.setMessages(Messages.DEPARTMENT_AUDIT_SUCCEED);
        return resultVo;
    }

    @Override
    @PreAuthorize("hasRole('ROLE_teacher_manager')")
    public ResultVo managerAudit(SuggestAuditDto suggestAuditDto) {
        ResultVo resultVo = new ResultVo();
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(suggestAuditDto.getApplyId());

        if (applyOfIP == null) {
            resultVo.setMessages(Messages.FIND_APPLYOFIP_BY_ID_NOT_FOUND);
            resultVo.setFailed();
            return resultVo;
        }

        if (applyOfIP.getState().getIsDepartmentAuditedPass()) {
            applyOfIP.setDepartmentSuggest(makeSuggest(suggestAuditDto.getSuggest()));
            applyOfIP.getState().setIsManagerAuditedPass(suggestAuditDto.getIsPass());
            applyOfIPRepository.save(applyOfIP);
            resultVo.setSucceeded();
            resultVo.setMessages(Messages.MANAGER_AUDIT_SUCCEED);
        } else {
            resultVo.setMessages(Messages.MANAGER_CANNOT_AUDIT_BY_DEPARTMENT_NO_AUDIT);
            resultVo.setFailed();
        }
        return resultVo;
    }

    @Override
    public ResultVo academicAudit(SuggestAuditDto suggestAuditDto) {
        ResultVo resultVo = new ResultVo();
        ApplyOfIP applyOfIP = applyOfIPRepository.findOne(suggestAuditDto.getApplyId());
        if (applyOfIP == null) {
            resultVo.setMessages((Messages.FIND_APPLYOFIP_BY_ID_NOT_FOUND));
            resultVo.setFailed();
            return resultVo;
        }

        State state = applyOfIP.getState();
        if (state.getIsDepartmentAuditedPass() && state.getIsManagerAuditedPass()) {
            applyOfIP.setAcademicSuggest(makeSuggest(suggestAuditDto.getSuggest()));
            applyOfIP.getState().setIsAcademicAuditedPass(suggestAuditDto.getIsPass());
            applyOfIPRepository.save(applyOfIP);
            resultVo.setSucceeded();
            resultVo.setMessages(Messages.ACADEMIC_AUDIT_SUCCEED);
        } else {
            resultVo.setFailed();
            resultVo.setMessages(Messages.ACADEMIC_AUDIT_FAIL);
        }
        return resultVo;
    }*/
        /*
        Teacher temp = new Teacher();
        temp.setUserType(UserType.teacher);
        temp.setType(JobType.departmentHead);
        if (user.getType().equals(temp.getType())) {
            departmentAudit(suggestAuditDto, user, applyOfIP, resultVo);
        }
        temp.setType(JobType.manager);
        if (user.getType().equals(temp.getType())) {
            managerAudit(suggestAuditDto, user, applyOfIP, resultVo);
        }
        temp.setType(JobType.chargeOfIP);
        if (user.getType().equals(temp.getType())) {
            academicAudit(suggestAuditDto, applyOfIP, resultVo);
        }
        User admin = new User();
        admin.setUserType(UserType.admin);
        if (user.getUserType().equals(admin.getUserType())) {
            adminAudit(suggestAuditDto, user, applyOfIP, resultVo);
        }

        * */  /*
        Teacher teacher = new Teacher();
        teacher.setUserType(UserType.teacher);
        teacher.setType(JobType.departmentHead);
        if (user.getType().equals(teacher.getType())) return true;
        teacher.setType(JobType.manager);
        if (user.getType().equals(teacher.getType())) return true;
        teacher.setType(JobType.chargeOfIP);
        if (user.getType().equals(teacher.getType())) return true;
        teacher.setType(JobType.leader);
        if (user.getType().equals(teacher.getType())) return true;
        User admin = new User();
        admin.setUserType(UserType.admin);
        return user.getType().equals(admin.getType());
        *//*
        Teacher teacher = new Teacher();
        teacher.setUserType(UserType.teacher);
        teacher.setType(JobType.departmentHead);
        if (type.equals(teacher.getType())) {
            state.setIsDepartmentAuditedPass(isPass);
        }
        teacher.setType(JobType.manager);
        if (type.equals(teacher.getType())) {
            state.setIsDepartmentAuditedPass(true);
            state.setIsManagerAuditedPass(isPass);
        }
        teacher.setType(JobType.chargeOfIP);
        if (type.equals(teacher.getType())) {
            state.setIsManagerAuditedPass(true);
            state.setIsDepartmentAuditedPass(true);
            state.setIsAcademicAuditedPass(isPass);
        }
        return state;
        */

        /*
        //转移上传的资料并将路径保存在申请里面
        if (fileService.transferInformationFile(applyOfIP,user)) {
            applyOfIPRepository.save(applyOfIP);
        } else {
            //转移失败设置创建申请失败信息并删除申请
            applyOfIPRepository.delete(applyOfIP);
            resultVo.setFailed();
            resultVo.setMessages(Messages.ADD_FAIL);
            return resultVo;
        }*/